package com.student.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.student.education.common.CommonPage;
import com.student.education.common.CommonResult;
import com.student.education.entity.Clazz;
import com.student.education.entity.Major;
import com.student.education.dao.MajorDao;
import com.student.education.service.MajorService;
import com.student.education.vo.MajorClassAddVo;
import com.student.education.vo.MajorClassSelectVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 专业表(Major)表服务实现类
 *
 * @author makejava
 * @since 2021-11-27 16:03:29
 */
@Service("majorService")
public class MajorServiceImpl implements MajorService {
    @Resource
    private MajorDao majorDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Major queryById(Integer id) {
        return this.majorDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Major> queryAllByLimit(int offset, int limit) {
        return this.majorDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param major 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(Major major) {
        return this.majorDao.insert(major);
    }

    /**
     * 修改数据
     *
     * @param major 实例对象
     * @return 实例对象
     */
    @Override
    public int update(Major major) {
        return this.majorDao.update(major);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        //删除 中间表和班级关联的数据
        majorDao.deleteMajorClass(id);
        return this.majorDao.deleteById(id) > 0;
    }

    @Override
    public CommonResult<List<Clazz>> selectNoMajorClass() {
        return CommonResult.success(majorDao.selectNoMajorClass());
    }

    @Override
    public int addMajorClass(MajorClassAddVo vo) {

        return majorDao.addMajorClass(vo);
    }

    @Override
    public int deleteMajorClass(Integer classId) {
        return majorDao.deleteMajorClassById(classId);
    }

    @Override
    public List<MajorClassSelectVo> listSelect() {
        List<MajorClassSelectVo> list = majorDao.listSelect();
        for (int i =0;i<list.size();i++){
            List<Clazz> clazzes = majorDao.selectClass(list.get(i).getId());
            list.get(i).setList(clazzes);
        }
        return list;
    }
}